package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.BtyMdYydz;
import cn.stylefeng.guns.sys.modular.system.entity.BtyMdYydzDetail;
import cn.stylefeng.guns.sys.modular.system.entity.BusConst;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyMdYydzDetailMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyMdYydzMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyWxMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.UserMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyMdYydzDetailParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyMdYydzDetailResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyMdYydzDetailService;
import cn.stylefeng.guns.sys.modular.system.service.PushMsgService;
import cn.stylefeng.guns.sys.modular.system.service.WeChatSendTempMsgService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 营业额报备详情表 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-03
 */
@Service
public class BtyMdYydzDetailServiceImpl extends ServiceImpl<BtyMdYydzDetailMapper, BtyMdYydzDetail> implements BtyMdYydzDetailService {

    private static Logger logger = LoggerFactory.getLogger(BtyMdYydzDetailServiceImpl.class);

    @Resource
    private UserMapper userMapper;
    @Resource
    private BtyMdYydzMapper mdYydzMapper;
    @Resource
    private WeChatSendTempMsgService weChatSendTempMsgService;
    @Resource
    private BtyWxMapper btyWxMapper;
    @Resource
    private PushMsgService pushMsgService;

    @Override
    public void add(BtyMdYydzDetailParam param){
        //获取用户id
        Long userId = LoginContextHolder.getContext().getUserId();
        //用户姓名
        String name = LoginContextHolder.getContext().getUser().getName();
        Date date = new Date();

        BtyMdYydzDetail entity = getEntity(param);

        entity.setPromoterType(1);//发起人:0->店长,1->应收会计
        entity.setCwApproveTime(date);//财务审核时间
        entity.setCreateTime(date);//创建时间
        entity.setCreateUser(userId);//创建者
        this.save(entity);

        //修改该店应收款会计
//        BtyMdYydz mdYydz = new BtyMdYydz();
////        mdYydz.setId(param.getYydzId());
//        mdYydz.setCode(param.getCode());
//        mdYydz.setYskj(name);
//        mdYydzMapper.updateByCode(mdYydz);
    }

    @Override
    public void delete(BtyMdYydzDetailParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyMdYydzDetailParam param){
        BtyMdYydzDetail oldEntity = getOldEntity(param);
        BtyMdYydzDetail newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);

        Date date = new Date();
        //获取用户id
        Long userId = LoginContextHolder.getContext().getUserId();

        //最终解决方式为空
        if (ToolUtil.isEmpty(newEntity.getFinalMethod())){
            newEntity.setCwApproveTime(date);//财务审核时间
        }
        newEntity.setUpdateTime(date);
        newEntity.setUpdateUser(userId);

        this.updateById(newEntity);
    }

    @Override
    public BtyMdYydzDetailResult findBySpec(BtyMdYydzDetailParam param){
        return null;
    }

    @Override
    public List<BtyMdYydzDetailResult> findListBySpec(BtyMdYydzDetailParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyMdYydzDetailParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * APP新增报备
     * @param param
     * @param account
     */
    @Override
    public void addReport(BtyMdYydzDetailParam param, String account) {

        //获取用户id
        Long userId = userMapper.getUserId(account);
        //获取营业额报备id
        int yydzId = mdYydzMapper.getId(param.getCode());
        Date date = new Date();

        BtyMdYydzDetail entity = getEntity(param);
        entity.setYydzId(yydzId);//营业对账id
        entity.setPromoterType(0);//发起人:0->店长,1->应收会计
        entity.setDzApplyTime(date);//店长报备时间
        entity.setCreateTime(date);//创建时间
        entity.setCreateUser(userId);//创建者
        this.save(entity);

    }

    /**
     * APP营业额报备记录列表
     * @param btyMdYydzDetailParam
     * @return
     */
    @Override
    public LayuiPageInfo reprotList(BtyMdYydzDetailParam btyMdYydzDetailParam) {

        Page pageContext = getPageContext();
        IPage page = this.baseMapper.reprotList(pageContext, btyMdYydzDetailParam);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * APP营业额对账待回复列表
     * @param btyMdYydzDetailParam
     * @return
     */
    @Override
    public LayuiPageInfo noReplyList(BtyMdYydzDetailParam btyMdYydzDetailParam) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.noReplyList(pageContext, btyMdYydzDetailParam);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * APP营业额对账已回复列表
     * @param btyMdYydzDetailParam
     * @return
     */
    @Override
    public LayuiPageInfo replyList(BtyMdYydzDetailParam btyMdYydzDetailParam) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.replyList(pageContext, btyMdYydzDetailParam);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * APP营业额对账回复
     * @param btyMdYydzDetailParam
     */
    @Override
    public void editReply(BtyMdYydzDetailParam btyMdYydzDetailParam, String account) {
        BtyMdYydzDetail oldEntity = getOldEntity(btyMdYydzDetailParam);
        BtyMdYydzDetail newEntity = getEntity(btyMdYydzDetailParam);
        ToolUtil.copyProperties(newEntity, oldEntity);
        Date date = new Date();
        //获取用户id
        Long userId = userMapper.getUserId(account);

        newEntity.setDzReplayTime(date);//店长回复时间
        newEntity.setUpdateTime(date);
        newEntity.setUpdateUser(userId);
        this.updateById(newEntity);
    }

//    /**
//     * 推送消息接口
//     * @param param
//     */
//    @Override
//    public void pushMsg(BtyMdYydzDetailParam param) {
//        //修改数据库状态
//        BtyMdYydzDetail oldEntity = getOldEntity(param);
//        BtyMdYydzDetail newEntity = getEntity(param);
//        ToolUtil.copyProperties(newEntity, oldEntity);
//        Date date = new Date();
//        //获取用户id
//        Long userId = LoginContextHolder.getContext().getUserId();
//        //获取用户姓名
//        String name = LoginContextHolder.getContext().getUser().getName();
//
//        newEntity.setUpdateUser(userId);
//        newEntity.setUpdateTime(date);
//        this.updateById(newEntity);
//        //获取该店信息
//        BtyMdYydz mdYydz = mdYydzMapper.getById(param.getYydzId());
//
//        mdYydz.setYskj(name);
//        String now = DateUtil.getNow();
//        //推送消息-督导
//        if (param.getDdMsg() == 1){
//            String data = "应收会计" + name + "提醒您，您的" + mdYydz.getDm() + "：" + param.getCwApprove() + "！请尽快联系店长" + mdYydz.getDz() + "处理！";
//            JSONObject tempObj = weChatSendTempMsgService.setTempParam(mdYydz.getDm(), null, "门店营业额对账", "门店营业额对账", now, data);
//
//            if (ToolUtil.isNotEmpty(mdYydz.getDdTel())){
//                //获取督导openid
//                User user = btyWxMapper.getOpenId(mdYydz.getDdTel());
//                String[] openid = null;
//                if (ToolUtil.isNotEmpty(user)) {
//                    openid = new String[]{user.getOpenid()};
//                }
//                weChatSendTempMsgService.sendmsg(openid, BusConst.MSURL, tempObj);
//                logger.info("======已推送督导公众号消息======");
//
//                //推送小妖消息
//                pushMsgService.yyddToIM(mdYydz, param, now);
//                logger.info("======已推送督导APP消息======");
//            }
//
//            //推送消息-店长
//        }else if (param.getDzMsg() == 1){
//            String data = "您的应收会计" + name + "提醒您：" + param.getCwApprove() + " 请尽快登录小妖APP，进入“门店对账菜单”，进行回复此问题！";
//            JSONObject tempObj = weChatSendTempMsgService.setTempParam(mdYydz.getDm(), null, "门店营业额对账", "门店营业额对账", now, data);
//
//            if (ToolUtil.isNotEmpty(mdYydz.getDzTel())){
//                //获取店长openid
//                User user = btyWxMapper.getOpenId(mdYydz.getDzTel());
//                String[] openid = null;
//                if (ToolUtil.isNotEmpty(user)) {
//                    openid = new String[]{user.getOpenid()};
//                }
//                weChatSendTempMsgService.sendmsg(openid, BusConst.MSURL, tempObj);
//                logger.info("======已推送店长公众号消息======");
//
//                //推送小妖消息
//                pushMsgService.yydzToIM(mdYydz, param, now);
//                logger.info("======已推送店长APP消息======");
//
//            }
//
//        }
//    }

    /**
     * 推送消息接口-督导
     * @param param
     */
    @Override
    public void pushDdMsg(BtyMdYydzDetailParam param) {
        //修改数据库状态
        BtyMdYydzDetail oldEntity = getOldEntity(param);
        BtyMdYydzDetail newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        Date date = new Date();
        //获取用户id
        Long userId = LoginContextHolder.getContext().getUserId();
        //获取用户姓名
//        String name = LoginContextHolder.getContext().getUser().getName();

        newEntity.setUpdateUser(userId);
        newEntity.setUpdateTime(date);
        this.updateById(newEntity);
        //获取该店信息
        BtyMdYydz mdYydz = mdYydzMapper.getById(param.getYydzId());

//        mdYydz.setYskj(name);
        String now = DateUtil.getNow();
        //推送消息-督导
        if (param.getDdMsg() == 1){
            String data = "应收会计" + mdYydz.getYskj() + "提醒您，您的" + mdYydz.getDm() + ",“" + param.getCwApprove() + "”，请尽快联系店长" + mdYydz.getDz() + "处理！";
            JSONObject tempObj = weChatSendTempMsgService.setTempParam(mdYydz.getDm(), null, "门店营业额对账", "门店营业额对账", now, data);

            if (ToolUtil.isNotEmpty(mdYydz.getDdTel())){
                //获取督导openid
                User user = btyWxMapper.getOpenId(mdYydz.getDdTel());
                String[] openid = null;
                if (ToolUtil.isNotEmpty(user)) {
                    openid = new String[]{user.getOpenid()};
                }
//                openid = new  String[]{"oJnvX6VsSUxsvbJOKbZCVmNz9Scc"};
                weChatSendTempMsgService.sendmsg(openid, BusConst.MSURL, tempObj);
                logger.info("======已推送督导公众号消息======");

                //推送小妖消息
                pushMsgService.yyddToIM(mdYydz, param, now);
                logger.info("======已推送督导APP消息======");
            }
        }
    }

    /**
     * 推送消息接口-店长
     * @param param
     */
    @Override
    public void pushDzMsg(BtyMdYydzDetailParam param) {
        //修改数据库状态
        BtyMdYydzDetail oldEntity = getOldEntity(param);
        BtyMdYydzDetail newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        Date date = new Date();
        //获取用户id
        Long userId = LoginContextHolder.getContext().getUserId();
        //获取用户姓名
//        String name = LoginContextHolder.getContext().getUser().getName();

        newEntity.setUpdateUser(userId);
        newEntity.setUpdateTime(date);
        this.updateById(newEntity);
        //获取该店信息
        BtyMdYydz mdYydz = mdYydzMapper.getById(param.getYydzId());

//        mdYydz.setYskj(name);
        String now = DateUtil.getNow();

            //推送消息-店长
        if (param.getDzMsg() == 1){
            String data = "您的应收会计" + mdYydz.getYskj() + "提醒您，“" + param.getCwApprove() + "”，请尽快登录小妖APP，进入“门店对账菜单”，进行回复此问题！";
            JSONObject tempObj = weChatSendTempMsgService.setTempParam(mdYydz.getDm(), null, "门店营业额对账", "门店营业额对账", now, data);

            if (ToolUtil.isNotEmpty(mdYydz.getDzTel())){
                //获取店长openid
                User user = btyWxMapper.getOpenId(mdYydz.getDzTel());
                String[] openid = null;
                if (ToolUtil.isNotEmpty(user)) {
                    openid = new String[]{user.getOpenid()};
                }
//                openid = new  String[]{"oJnvX6VsSUxsvbJOKbZCVmNz9Scc"};
                weChatSendTempMsgService.sendmsg(openid, BusConst.MSURL, tempObj);
                logger.info("======已推送店长公众号消息======");

                //推送小妖消息
                pushMsgService.yydzToIM(mdYydz, param, now);
                logger.info("======已推送店长APP消息======");

            }

        }
    }

    /**
     * APP获取当前日期
     */
    @Override
    public ResponseData now() {
        String now = DateUtil.getNow();
        now = now.substring(0,7);
        return ResponseData.success(now);
    }

    private Serializable getKey(BtyMdYydzDetailParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyMdYydzDetail getOldEntity(BtyMdYydzDetailParam param) {
        return this.getById(getKey(param));
    }

    private BtyMdYydzDetail getEntity(BtyMdYydzDetailParam param) {
        BtyMdYydzDetail entity = new BtyMdYydzDetail();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
